package com.itcaius.myandroidhomework.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.itcaius.myandroidhomework.R;
import com.itcaius.myandroidhomework.data.model.Book;

import java.util.List;

import lombok.Getter;

/**
 * Created by itcaius on 2025/3/28.
 * 图书适配器将图文嵌套
 */
public class BookAdapter extends BaseAdapter {
    private Context context;
    private List<Book> bookList;

    public BookAdapter(Context context, List<Book> bookList) {
        this.context = context;
        this.bookList = bookList;
    }

    public List<Book> getBookList() {
        return bookList;
    }

    @Override
    public int getCount() {
        return bookList.size();
    }

    @Override
    public Object getItem(int position) {
        return bookList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if(convertView == null){
            convertView = LayoutInflater.from(context).inflate(R.layout.item_book, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.bookImage = convertView.findViewById(R.id.book_image);
            viewHolder.bookTitle = convertView.findViewById(R.id.book_title);
            viewHolder.bookDescription = convertView.findViewById(R.id.book_description);
            viewHolder.bookCheckBtn = convertView.findViewById(R.id.book_select_btn);
            convertView.setTag(viewHolder);
        }else{
            viewHolder = (ViewHolder) convertView.getTag();
        }
        Book book = bookList.get(position);

        viewHolder.bookImage.setImageResource(book.getImageResId());
        viewHolder.bookTitle.setText(book.getTitle());
        viewHolder.bookDescription.setText(book.getDescription());

        if (book.isSelected()) {
            viewHolder.bookCheckBtn.setText("取消");
        } else {
            viewHolder.bookCheckBtn.setText("选课");
        }

        //点击事件逻辑修改(替换原来的点击事件)
        viewHolder.bookCheckBtn.setOnClickListener(v->{
            if(book.isSelected()){
                book.setSelected(false);
                viewHolder.bookCheckBtn.setText("选课");
                Log.i("图书状态", "取消了" + book.getTitle()+"状态"+book.isSelected().toString());
                Toast.makeText(context, "你取消了" + book.getTitle(), Toast.LENGTH_SHORT).show();
            }else{
                book.setSelected(true);
                viewHolder.bookCheckBtn.setText("取消");
                Log.i("图书状态", "选择了" +book.getTitle()+"状态"+ book.isSelected().toString());
                Toast.makeText(context, "你选择了" + book.getTitle(), Toast.LENGTH_SHORT).show();
            }
        });

        return convertView;
    }
    //嵌套数据的实现类
    static class ViewHolder{
        ImageView bookImage;
        TextView bookTitle;
        TextView bookDescription;
        Button bookCheckBtn;
    }
}
